var express = require('express');
var router = express.Router();

const db = require("../public/javascripts/db")

/* GET home page. */
router.get('/', function (req, res, next) {
    var info = req.query;
    var page = req.query.page || 1;
    var size = req.query.size || 5;
    var start = (page - 1) * size;
    let str = " limit " + start + "," + size;
    let sql = "select * from express";
    let sql2 = "select count(*) from express";
    let data = null;
    if (info.express_code && info.express_company && info.express_status) {
        sql += " where express_code like ? and express_company=? and express_status=? order by express_id desc" + str;
        sql2 += " where express_code like ? and express_company=? and express_status=?";
        data = ["%" + info.express_code + "%", info.express_company,info.express_status]
    }else if (info.express_code && info.express_company) {
        sql += " where express_code like ? and express_company=? order by express_id desc" + str;
        sql2 += " where express_code like ? and express_company=?";
        data = ["%" + info.express_code + "%", info.express_company]
    }else if (info.express_code && info.express_status) {
        sql += " where express_code like ? and express_status=? order by express_id desc" + str;
        sql2 += " where express_code like ? and express_status=?";
        data = ["%" + info.express_code + "%", info.express_status]
    }else if (info.express_status && info.express_company) {
        sql += " where express_status=? and express_company=? order by express_id desc" + str;
        sql2 += " where express_status=? and express_company=?";
        data = [info.express_status, info.express_company]
    }else if (info.express_code) {
        sql += " where express_code like ? order by express_id desc" + str;
        sql2 += " where express_code like ?";
        data = ["%" + info.express_code + "%"]
    } else if (info.express_company) {
        sql += " where express_company=? order by express_id desc" + str;
        sql2 +=" where express_company=?";
        data = [info.express_company]
    } else if (info.express_status) {
        sql += " where express_status=? order by express_id desc" + str;
        sql2 +=" where express_status=?";
        data = [info.express_status]
    }else{
        sql += " order by express_id desc" + str;
    }
    db(sql2, data, (result) => {
        db(sql, data, (result2) => {
            if (result2.length >= 0) {
                res.json({
                    code: 200,
                    message: "查询成功",
                    data: result2,
                    total:result[0]["count(*)"]
                })
            } else {
                res.json({
                    code: 500,
                    message: "服务器错误"
                })
            }
        })
    })


});

module.exports = router;